package com.zoran.study.spring.configuration;

import com.zoran.study.spring.controller.filter.AuthCheckFilter;
import com.zoran.study.spring.controller.filter.RequestLoggingFilter;
import com.zoran.study.spring.controller.interceptors.MyFirstInterceptor;
import com.zoran.study.spring.controller.interceptors.MySecondInterceptor;
import com.zoran.study.spring.controller.interceptors.RequestLoggingInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * create by @author zouxm on 16/12/21.
 */
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {

    //filter配置
    @Bean
    public FilterRegistrationBean requestLoggingFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("requestLoggingFilter");
        registrationBean.setFilter(new RequestLoggingFilter());
        registrationBean.setAsyncSupported(true);
        registrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean requestAuthCheckFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("requestAuthCheckFilter");
        registrationBean.setFilter(new AuthCheckFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 15);
        return registrationBean;
    }

    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new MyFirstInterceptor()).order(1);
//        registry.addInterceptor(new MySecondInterceptor()).order(2);
        registry.addInterceptor(new RequestLoggingInterceptor()).order(3);
    }

    /*@Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable("default_servlet");
    }*/

    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxUploadSize(524288000);
        multipartResolver.setMaxInMemorySize(40960);
        return multipartResolver;
    }
}
